<?php


namespace common\services;

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use AlibabaCloud\Mts\Mts;
use common\helpers\Util;
use Yii;

/**
 * 媒体转码服务
 * Class MpsService
 * @package common\services
 */
class MpsService
{

    private $accessKey;

    private $accessSecret;

    private $_base_uri;

    private $bucket;

    //数据中心
    private $region;

    public function __construct()
    {
        $this->accessKey = \Yii::$app->params['mps_conf']['accessKeyId'];
        $this->accessSecret = \Yii::$app->params['mps_conf']['accessKeySecret'];
        $this->_base_uri = 'http://mts.cn-hangzhou.aliyuncs.com';

        $this->bucket = \Yii::$app->params['oss_conf']['bucket'];
        $this->region = \Yii::$app->params['oss_conf']['region'];

        // 设置全局客户端
        AlibabaCloud::accessKeyClient($this->accessKey, $this->accessSecret)
            ->regionId('cn-shanghai')
            ->asDefaultClient();
    }

    //获取转码列表
    public function searchStsList()
    {
        try {
            // 访问产品 APIs
            $request = Mts::v20140618()
                ->ListJob()
                ->format('json')
                ->withState('All')
                ->request();
            return $request->toArray();
        } catch (ClientException $exception) {
            \Yii::error('Client:查询媒体列表失败.' . Util::exceptionToString($exception));
            throw $exception;
        } catch (ServerException $exception) {
            \Yii::error('Server:查询媒体列表失败.' . Util::exceptionToString($exception));
            throw $exception;
        }
    }


    /**
     * 查询视频信息
     * @param $ossVideoUrl
     * @return array
     * @throws ClientException
     * @throws ServerException
     */
    public function queryMedia($ossVideoUrl)
    {
        try {
            // 访问产品 APIs
            $request = Mts::v20140618()
                ->queryMediaListByURL()
                ->format('json')
                ->withFileURLs($ossVideoUrl)
                ->request();
            return $request->toArray();
        } catch (ClientException $exception) {
            \Yii::error('Client:查询媒体列表失败.' . Util::exceptionToString($exception));
            throw $exception;
        } catch (ServerException $exception) {
            \Yii::error('Server:查询媒体列表失败.' . Util::exceptionToString($exception));
            throw $exception;
        }

    }

    public function getMediaInfo($ossVideoUrl)
    {
        try {
            $ossHost = \Yii::$app->params['oss_conf']['cndHost'];
            $prefix = \Yii::$app->params['mps_conf']['sts_prefix'];


            //拼接oss源地址(此处因为需要通过oss源地址，请求转码信息)
            $parseUrl = parse_url($ossVideoUrl);
            $ossVideoUrl = 'http://'.Yii::$app->params['oss_conf']['bucket'].'.'.Yii::$app->params['oss_conf']['endpoint'].$parseUrl['path'];

            $queryInfo = $this->queryMedia($ossVideoUrl);

            $videoInfo['size'] = $queryInfo['MediaList']['Media'][0]['Size']??'';
            $videoInfo['fps'] = $queryInfo['MediaList']['Media'][0]['Fps']??'';
            $videoInfo['duration'] = $queryInfo['MediaList']['Media'][0]['Duration']??'';
            $videoInfo['bitrate'] = $queryInfo['MediaList']['Media'][0]['Bitrate']??'';
            $videoInfo['coverURL'] = $queryInfo['MediaList']['Media'][0]['CoverURL']??'';
            $videoInfo['height'] = $queryInfo['MediaList']['Media'][0]['Height']??'';
            $videoInfo['width'] = $queryInfo['MediaList']['Media'][0]['Width']??'';
            $videoInfo['originUrl'] = $queryInfo['MediaList']['Media'][0]['File']['URL']??'';
            //转码地址
            $runId = $queryInfo['MediaList']['Media'][0]['RunIdList']['RunId'][0]??'';
            $file = $queryInfo['MediaList']['Media'][0]['Title'];
            $videoInfo['mediaUrl'] = $ossHost.'/'.$prefix.'/'.$runId.'/'.$file;

            return $videoInfo;
        } catch (\Throwable $throwable) {
            throw $throwable;
        }
    }


}